package com.itheima.edu.practice.demo;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo02 {
    //创建一个HashMap集合，键是学生对象，值是籍贯，存储三个键值对元素并遍历
    public static void main(String[] args) {
        HashMap<Student, String> hashMap = new HashMap<Student, String>();
        hashMap.put(new Student("龙门", 20), "湖南");
        hashMap.put(new Student("安全", 22), "四川");
        hashMap.put(new Student("所以", 24), "北京");

        //遍历方法一
        Set<Student> students = hashMap.keySet();
        for (Student s : students) {
            System.out.println(s + hashMap.get(s));
        }
        System.out.println("---------");

        //遍历方法二
        Set<Map.Entry<Student, String>> mapEntry =hashMap.entrySet();
        for (Map.Entry<Student, String> entry : mapEntry) {
            System.out.println(entry.getKey()+"---"+entry.getValue());
        }
        System.out.println("----------");
        //方法三
        hashMap.forEach(
                (Student student ,String s)->{
                    System.out.println(student+"---"+s);
                }
        );
    }
}
